/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CGamePlayState.h
//
//		Author			:		Kevin Jordan
//
//		Purpose			:		A header for all functions and members of the Game Play State
//
//		Dates			:		Written( Feb 12, 2011 ) by Kevin Jordan		LastUpdate( Feb 17, 2011  ) by Kevin Jordan 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CGamePlayState.h"
#include "CMainMenuState.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "VIN_EventSystem.h"
#include "VIN_ObjectManager.h"
#include "CSaveOrLoadState.h"
#include "CJCEvent.h"
#include "CGame.h"
#include "CEnemy.h"
#include "CParticle.h"
#include "CPowerUp.h"
#include "CTestingLevelState.h"
#include "CDocksLevelState.h"
#include "CFactoryLevelState.h"
#include "CFarmLevelState.h"
#include "CCityLevelState.h"
#include "CHubState.h"
#include "CHighScoreState.h"
#include "VIN_BitMapFont.h"


CGamePlayState::CGamePlayState()
{
	HUDID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/HUD.png");

	m_bTriggedEvent = false;
	pLevelState = NULL;

	m_bGamePaused = false;
	IsInGame = false;
	PauseID = 0;
	m_nPauseSelection = 0;
	score = 0;

}

CGamePlayState::~CGamePlayState()
{



}


void CGamePlayState::Enter()
{

	VIN_EventSystem::GetInstance()->ResgisterClient("DynTrig",this);

	int LevelToEnter = CHubState::GetInstance()->GetLevel();

	CHubState::GetInstance()->GetPlayer()->SetLives(3);

	PauseID  = CSGD_TextureManager::GetInstance()->LoadTexture( "./resource/Graphics/Pause_Picture.png");

	switch(LevelToEnter)
	{
	case 0:
		{
			pLevelState  = CTestingLevelState::GetInstance();
		}
		break;
	case 1:
		{
			pLevelState  = CDocksLevelState::GetInstance();
		}
		break;
	case 2:
		{
			pLevelState  = CFarmLevelState::GetInstance();
		}
		break;
	case 3:
		{
			pLevelState  = CFactoryLevelState::GetInstance();
		}
		break;
	case 4:
		{
			pLevelState  = CCityLevelState::GetInstance();
		}
		break;
	}

	if(pLevelState)
	{
		pLevelState->Enter();

		IsInGame = true;	
	}
}


void CGamePlayState::Exit() 
{

	m_bTriggedEvent = false;
	VIN_EventSystem::GetInstance()->UnregisterClient("DynTrig",this);

	pLevelState->Exit();

	IsInGame = false;
	m_bWinGame = false;
	m_bGameOver = false;
	m_bGamePaused = false;
}


CGamePlayState* CGamePlayState::GetInstance()
{
	static CGamePlayState instance;
	return &instance;
}

void CGamePlayState::Render()
{
	CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
	VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pD3D->Clear(0,0,0);
	if( !m_bGamePaused )
	{
		pLevelState->Render();


		if( !CTestingLevelState::GetInstance()->GetInShop() )
		{
			VIN_ObjectManager::GetInstance()->RenderObjects();
		}

		if(m_bWinGame)
		{
			VIN_ObjectManager::GetInstance()->RemoveAllObjects();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
			
			RECT WinScreen;
			SetRect(&WinScreen, 0,0, pD3D->GetPresentParams()->BackBufferWidth, pD3D->GetPresentParams()->BackBufferHeight);
			pD3D->DrawRect(WinScreen, 128, 128, 128);

			pBF->Render("You WIN!!!", 220,100,2.0f, D3DCOLOR_ARGB(255,0,0,0),MENUFONT);

			char timebuff[100];
			// TODO: put in Variable
			pBF->Render(timebuff, 250,250,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);

			char scorebuff[100];
			// TODO: put in Variable
			sprintf_s(scorebuff, sizeof(scorebuff), "Total Score : %d", score);
			pBF->Render(scorebuff, 250,300,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);
		}
		if(m_bGameOver)
		{
			CHubState::GetInstance()->GetPlayer()->SetLives(3);
			VIN_ObjectManager::GetInstance()->RemoveAllObjects();
			VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
			
			RECT LoseScreen;
			SetRect(&LoseScreen, 0,0, pD3D->GetPresentParams()->BackBufferWidth, pD3D->GetPresentParams()->BackBufferHeight);
			pD3D->DrawRect(LoseScreen, 255, 0, 0);
			pBF->Render("GAME OVER!!!", 220,100,1.75f, D3DCOLOR_ARGB(255,0,0,0),MENUFONT);

			char scorebuff[100];
			// TODO: put in Variable
			sprintf_s(scorebuff, sizeof(scorebuff), "Total Score : %d", score);
			pBF->Render(scorebuff, 250,300,.75f, D3DCOLOR_ARGB(255,0,0,0),GAMEFONT);
		}
	}
	else if( m_bGamePaused )
	{
		VIN_SoundManager::GetInstance()->StopSound(VIN_SoundManager::GetInstance()->GetAlarm());
		
		pTM->Draw( PauseID, 0,0,1.0f,1.0f,0,0.0f,0.0f,0.0f, D3DCOLOR_ARGB(128,0,0,0));
		pBF->Render( "PAUSE", 300, 200, 2, D3DCOLOR_ARGB(255,255,0,0), GAMEFONT);

		pBF->Render( "RESUME", 350, 255, 1, D3DCOLOR_ARGB(255,255,0,0), MENUFONT);
		pBF->Render( "QUIT LEVEL", 350, 300, 1, D3DCOLOR_ARGB(255,255,0,0), MENUFONT);

		switch( m_nPauseSelection )
		{
		case 0:
			{
				pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 300,260,2.0f,1.0f,0,0.0f,0.0f,0.0f, D3DCOLOR_ARGB(255,255,0,0));
			}
			break;
		case 1:
			{
				pTM->Draw( CMainMenuState::GetInstance()->GetCursorID(), 300, 305, 2.0f,1.0f,0,0.0f,0.0f,0.0f, D3DCOLOR_ARGB(255,255,0,0));
			}
			break;
		}
	}
}

bool CGamePlayState::Input()
{
	CSGD_DirectInput*	pDI = CSGD_DirectInput::GetInstance();
	CGame*				pGameState = CGame::GetInstance();

	if( !m_bGamePaused )
	{	
		if( !CTestingLevelState::GetInstance()->GetInShop() )
			{
				VIN_ObjectManager::GetInstance()->InputObjects();
			}
			pLevelState->Input();
	}
	/*if( !CTestingLevelState::GetInstance()->GetInShop() )
	{

		CGamePlayState::GetInstance()->WinGame(false);
		CGamePlayState::GetInstance()->GameOver(false);
	}*/

	if( m_bGamePaused )
	{	
		/*if( pDI->KeyPressed( DIK_ESCAPE ))
			m_bGamePaused = !m_bGamePaused;*/

		if( pDI->KeyPressed(DIK_UP))
		{	
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

			m_nPauseSelection--;
			if( m_nPauseSelection < 0 )
				m_nPauseSelection = 1;
		}
		else if( pDI->KeyPressed(DIK_DOWN))
		{
				VIN_SoundManager::GetInstance()->PlayMenuSelect();

			m_nPauseSelection++;
			if( m_nPauseSelection > 1 )
				m_nPauseSelection = 0;		
		}
		if( pDI->KeyPressed(DIK_RETURN))
		{
				VIN_SoundManager::GetInstance()->PlayMenuConfirm();

			switch( m_nPauseSelection )
			{
			case 0:
				{
					m_bGamePaused = false;				
					m_nPauseSelection = 0;
				}
				break;
			case 1:
				{
					CGame::GetInstance()->ChangeState(CHubState::GetInstance());
					m_bGamePaused = false;
					m_nPauseSelection = 0;
				}
				break;
			}
		}
	}
	return true;
}

void CGamePlayState::Update(float fElapsedTime)
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	if( !m_bGamePaused )
	{
		pLevelState->Update(fElapsedTime);

		VIN_EventSystem::GetInstance()->ProcessEvents();

		static float ScreenTime = 0;

		if(CGamePlayState::GetInstance()->GetWin())
		{
			ScreenTime += fElapsedTime;

			if(ScreenTime > 10 || pDI->KeyPressed( DIK_ESCAPE ) || pDI->KeyPressed( DIK_RETURN ))
			{
				m_bGameOver = m_bWinGame = false;
				ScreenTime = 0;
				CalculateScore( CTestingLevelState::GetInstance()->GetWaveCount(), CHubState::GetInstance()->GetPlayer()->GetCash(), (int)CTestingLevelState::GetInstance()->GetWaveTime(),  (int)CHubState::GetInstance()->GetPlayer()->GetRageAmt(),  (int)CHubState::GetInstance()->GetPlayer()->GetHP());
				if(CHighScoreState::GetInstance()->LowestScore() < score )
				{
					CHighScoreState::GetInstance()->Achievement(true, score);
					score = 0;
				}
				CGame::GetInstance()->ChangeState(CHighScoreState::GetInstance());
			}
		}

		if(CGamePlayState::GetInstance()->GetLose())
		{
			ScreenTime += fElapsedTime;
			if(ScreenTime > 10 || pDI->KeyPressed( DIK_ESCAPE ) || pDI->KeyPressed( DIK_RETURN ))
			{
				m_bGameOver = m_bWinGame = false;
				ScreenTime = 0;
				if(CHighScoreState::GetInstance()->LowestScore() < score )
				{
					CHighScoreState::GetInstance()->Achievement(true, score);
					score = 0;
				}
				CGame::GetInstance()->ChangeState(CHighScoreState::GetInstance());
			}
		}
	}
}

void CGamePlayState::UpdatePlayerProfile()
{
	/*CSaveOrLoadState::GetInstance()->FillProfile( CGame::GetInstance()->SaveSlot, ActiveProfile.GetLevel(), 
	ActiveProfile.GetHealth(), ActiveProfile.SetDurability(), ActiveProfile.SetCombat, ActiveProfile.SetSpeed(), ActiveProfile.SetSpeed() );*/
}

void CGamePlayState::HandleEvent(CEvent* pEvent)
{	
	if( pEvent->GetEventID() == "DynTrig" )
	{
		if( pEvent->GetParam() == this )
			m_bTriggedEvent = true;
	}
}

void CGamePlayState::RenderHUD()
{
	if( m_bGamePaused )
	{

	}
	else
	{
		CSGD_Direct3D* pD3D = CSGD_Direct3D::GetInstance();
		CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
		VIN_BitMapFont* pBF = VIN_BitMapFont::GetInstance();

		CPlayer* cPly = CHubState::GetInstance()->GetPlayer();

		//HUD BackGround
		pTM->Draw(HUDID, 0,0,(float)pD3D->GetPresentParams()->BackBufferWidth,1.0f, 0,0,0,0, D3DCOLOR_ARGB(200,0,0,0));
		//Health 
		pTM->Draw(HUDID, 85,20,(float)cPly->GetMaxHealth() * 2,0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,255,0,0));

		if(cPly->GetHP() >= 0)
			pTM->Draw(HUDID, 85,20,(float)cPly->GetHP() * 2,0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));

		//Rage
		pTM->Draw(HUDID, 85, 40,100,0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,255,0,0));

		if(cPly->GetRageAmt() > 0)
		{
			if(cPly->GetRageAmt() == 100)
				pTM->Draw(HUDID, 85,40,(float)cPly->GetRageAmt(),0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));
			else
				pTM->Draw(HUDID, 85,40,(float)cPly->GetRageAmt(),0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,255));
		}
		else
			pTM->Draw(HUDID, 85,40,0,0.30f, 0,0,0,0, D3DCOLOR_ARGB(255,0,255,0));

		pD3D->GetSprite()->Flush();

		char buffer[128];
		char buffer2[128];

		//sprintf_s(buffer, sizeof(buffer), "Lives: %d Money: $%d\n", 
		sprintf_s(buffer, sizeof(buffer), "Lives: %d Wave: %d Time: %.2f", 
			cPly->GetLives(), CTestingLevelState::GetInstance()->GetWaveCount(), CTestingLevelState::GetInstance()->GetWaveTime() );
		sprintf_s(buffer2, sizeof(buffer2), "Money: %d Round: %d", cPly->GetCash(), CTestingLevelState::GetInstance()->GetRoundCount() );
		
		pBF->Render( buffer, 300, 25, 0.5f, D3DCOLOR_ARGB(255,0,255,0), GAMEFONT);
		pBF->Render( buffer2, 300, 45, 0.5f, D3DCOLOR_ARGB(255,0,255,0), GAMEFONT);
		pBF->Render( "Health", 85, 20, 0.75f, .75f , D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
		pBF->Render( "Rage", 85, 40, 0.75f, .75f,D3DCOLOR_ARGB(255,0,0,0), GAMEFONT);
	}
	
}
int CGamePlayState::CalculateScore(int wave, int money, int timer, int rage, int health)
{
	score = (wave * 1000) + (money * 2) + (timer * 3) + (rage * 3) + (health * 2);
	if(score < 0)
		score = 0;
	return score;
}